﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;

/// <summary>
/// 制造商维护
/// Update By:Allen Jiang
/// Update Time:2008-12-22
/// Description:
///     修改制造商维护在一个页面。
/// </summary>
public partial class masterdata_view_manufacturers : PageBase
{
    ManufacturersManager manager = new ManufacturersManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //将分页控件指定到GridView
            ManuFactBind();
            
        }
    }

    /// <summary>
    /// 数据绑定
    /// </summary>
    public void ManuFactBind()
    {
        Jumpton.Persistent.ObjectTable ot= new ManufacturersManager().GetManufacturersTable(MyStateManager.DomainID,null);
        GridPager1.DataSource = ot;
        GridPager1.GridView = GridView1;
        GridPager1.BindData();
     
        GridPager1.Visible = GridView1.Rows.Count > 0;

        DetailsView1.ChangeMode(DetailsViewMode.Insert);
        DetailsView1.DataBind();
    }
     

    protected void DetailsView1_DataBound(object sender, EventArgs e)
    {
        CheckBox chk = DetailsView1.FindControl("chkValid") as CheckBox;
        TextBox ctl = DetailsView1.FindControl("txtCode") as TextBox;

        if (DetailsView1.DataItem == null)
        {
            chk.Checked = true;
        }
        else
        {
            ctl.Enabled=false;
        }
    }
    
    /// <summary>
    /// 数据保存方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            MANUFACTURERS manu = null;
            ManufacturersManager manager = new ManufacturersManager();
            TextBox ctlcode = DetailsView1.FindControl("txtCode") as TextBox;
            TextBox ctl = DetailsView1.FindControl("txtName") as TextBox;
            if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
            {
                manu = new MANUFACTURERS();

                //判断是否存在代码
                if (manager.LoadDomianID(ctlcode.Text, MyStateManager.DomainID) != null)
                {
                    ShowMessage("已存在此代码！");
                    return;
                }
                //判断是否存在名称
                if (manager.LoadName(ctl.Text, MyStateManager.DomainID) != null)
                {
                    ShowMessage("已存在此名称！");
                    return;
                }
                manu.ManuCode = ctlcode.Text;
            }
            else
            {
                MANUFACTURERS c = manager.LoadName(ctl.Text, MyStateManager.DomainID);
                //判断是否已经存在此名称
                if (c != null)
                {
                    if (!c.ManuCode.Equals(ctlcode.Text))
                    {
                        ShowMessage("已存在此名称");
                        return;
                    }
                }
                manu = manager.Load(int.Parse(GridView1.SelectedDataKey.Value.ToString()));
            }
           
            manu.ManuName = ctl.Text;

            ctl.Dispose();
            ctl = null;

            CheckBox cbxValid = DetailsView1.FindControl("chkValid") as CheckBox;
            CheckBox cbxcbxContender = DetailsView1.FindControl("cbxContender") as CheckBox;
            manu.ACTIVE = cbxValid.Checked;
            manu.CONTENDER = cbxcbxContender.Checked;
            manu.DOMAINID = MyStateManager.DomainID;
            manu.LAST_UPDATER = MyStateManager.UserID.ToString();
            manu.LAST_UPDATE_TIME = long.Parse(Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeString(DateTime.Now));
           

            string LogContent = "";

            if(manu.State == DataRowState.Added)
            {
                LogContent += "增加新的制造商，" + ctlcode.Text + "的信息是：";
                manu.CREATETIME = long.Parse(Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeString(DateTime.Now));
            }
            else
            {
                LogContent += manu.ManuCode + "信息发生变动，变动后信息是：";
            }
            LogContent += "代码：" + manu.ManuCode + "，";
            LogContent += "制造商名称：" + manu.ManuName + "，";
            LogContent += "有效：" + manu.ACTIVE + "，";
            LogContent += "竞争者：" + manu.CONTENDER;
            LogContent += "操作人：" + manu.LAST_UPDATER;

            manager.Save(manu);
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
             Jumpton.SFA.Server.BusinessRule.System.LogType.Info_Change, this.MyStateManager.Name,
             this.MyStateManager.UserID, LogContent);
            if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
            {
                ManuFactBind();
            }
            //else
            //{
               
            //}
            Response.Redirect(Request.RawUrl.ToString());
           
        }
        catch (Exception ex)
        {
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
                    Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, this.MyStateManager.Name,
                    this.MyStateManager.UserID, "厂商维护 - 异常:" + ex.Message);
        }

    }

  

    /// <summary>
    /// 编辑
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Edit);
        DetailsView1.DataBind();
       // ScriptManager.RegisterStartupScript(this, GetType(), "","<script>alert(1)</script>", true);
        
      
    }

    /// <summary>
    /// 新增方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Insert);
        DetailsView1.DataBind();
        ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "", "<script>open1();</script>", true);
       
       
    }


    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        ManuFactBind();
    }
    protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
    }
}
